package com.whmskj.xjlhsc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whmskj.xjlhsc.mapper.EquipmentMtnPlanAllocationMapper;
import com.whmskj.xjlhsc.entity.EquipmentMtnPlanAllocation;
import com.whmskj.xjlhsc.service.EquipmentMtnPlanAllocationService;
import com.whmskj.xjlhsc.utils.SCUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author lk
 * @since 2025-06-10 14:48:04
 */
@Service
@Slf4j
public class EquipmentMtnPlanAllocationServiceImpl extends ServiceImpl<EquipmentMtnPlanAllocationMapper, EquipmentMtnPlanAllocation> implements EquipmentMtnPlanAllocationService {

    @Autowired
    private EquipmentMtnPlanAllocationMapper equipmentMtnPlanAllocationMapper;

    @Override
    public Page<EquipmentMtnPlanAllocation> findPage(EquipmentMtnPlanAllocation params) {
        LambdaQueryWrapper<EquipmentMtnPlanAllocation> query = Wrappers.lambdaQuery(EquipmentMtnPlanAllocation.class);
        return equipmentMtnPlanAllocationMapper.selectPage(SCUtil.getPage(), query);
    }

    @Override
    public List<EquipmentMtnPlanAllocation> findList(EquipmentMtnPlanAllocation params) {
        LambdaQueryWrapper<EquipmentMtnPlanAllocation> query = Wrappers.lambdaQuery(EquipmentMtnPlanAllocation.class);
        return equipmentMtnPlanAllocationMapper.selectList(query);
    }

    @Override
    public EquipmentMtnPlanAllocation findById(Long id) {
        return equipmentMtnPlanAllocationMapper.selectById(id);
    }

    @Override
    public boolean insert(EquipmentMtnPlanAllocation equipmentMtnPlanAllocation) {
        return save(equipmentMtnPlanAllocation);
    }

    @Override
    public boolean update(EquipmentMtnPlanAllocation equipmentMtnPlanAllocation) {
        return updateById(equipmentMtnPlanAllocation);
    }

    @Override
    public int delete(Long id) {
        return equipmentMtnPlanAllocationMapper.deleteById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void copyGeneratorNext(Long maintenancePlanId, Long targetId) {
        LambdaQueryWrapper<EquipmentMtnPlanAllocation> qe = Wrappers.lambdaQuery();
        qe.eq(EquipmentMtnPlanAllocation::getWbPlanId, maintenancePlanId);
        List<EquipmentMtnPlanAllocation> oldList = list(qe);
        CopyOptions copyOptions = new CopyOptions();
        copyOptions.setIgnoreProperties("allocaId", "wbPlanId");
        List<EquipmentMtnPlanAllocation> newList = BeanUtil.copyToList(oldList, EquipmentMtnPlanAllocation.class, copyOptions);
        newList.forEach(cur -> cur.setWbPlanId(targetId));
        saveBatch(newList);
    }
}